<?php

namespace app\common\validate;

use think\Validate;

class Funds extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
    	'user_id' => 'require',
	    'account_id' => 'require|checkAccountId',
	    'operation_type' => 'require|in:1,2',
	    'funds_type_id' => 'require|checkFundsTypeId',
	    'amount' => 'require|float|notin:0',
    ];
    /**
     * 提示消息
     */
    protected $message = [
    	'user_id.require' => '用户不能为空',
	    'account_id.require' => '请选择帐户',
	    'operation_type' => '操作类型错误',
	    'funds_type_id.require' => '请选择消费类型',
	    'amount' => '请填写金额',
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];
    
    
    protected function checkAccountId($value, $rule, $data) {
    	$model = new \app\common\model\UserAccount();
    	$row = $model->where(['id' => $value, 'user_id' => $data['user_id']])->find();
    	if(!$row) {
    		return '帐户不存在';
	    }
    	return true;
    }
	
	protected function checkFundsTypeId($value) {
		$model = new \app\common\model\FundsType();
		$row = $model->find($value);
		if(!$row) {
			return '流水类型不存在';
		}
		return true;
	}
}
